<template>
  <section class="page">
    <headerbar>个人信息</headerbar>


    <section class="list-arrow mgt-2">
      <div class="item">
        <span class="title">姓名</span>
        <span class="title">{{userInfo.nickname}}</span>
      </div>
      <div class="item">
        <span class="title">性别</span>
        <span class="title">{{getSex(userInfo.sex)}}</span>
      </div>
      <div class="item">
        <span class="title">年龄</span>
        <span class="title">{{getAge(userInfo.birthday)}}</span>
      </div>
      <div class="item" v-if="partyInfo">
        <span class="title">民族</span>
        <span class="title">{{partyInfo.mz}}</span>
      </div>
      <div class="item" v-if="partyInfo" v-show="hasPermission">
        <span class="title">入党日期</span>
        <span class="title text-blue" @click="showTimePicker(partyInfo.rdsj)">{{getTime(partyInfo.rdsj)}}</span>
         <!-- @click="showTimePicker()" -->
      </div>
      <div class="item" v-if="partyInfo" v-show="hasPermission">
        <span class="title">党员类别</span>
        <span class="title">{{partyInfo.partyType}}</span>
      </div>
      <div class="item" v-if="partyInfo">
        <span class="title">工作单位</span>
        <span class="title">{{partyInfo.gzdw}}</span>
      </div>
      <div class="item" @click="go('modifyPwd')">
        <span class="title">修改密码</span>
        <i class="arrow"></i>
      </div>
      <div class="item" @click="go('modifyMobile')">
        <span class="title">修改手机号</span>
        <!--<span class="title">{{partyInfo.mobile}}</span>-->
        <i class="arrow"></i>
      </div>
      <div class="item" @click="go('changeAvatar')">
        <span class="title">更换头像</span>
        <i class="arrow"></i>
      </div>
    </section>

  </section>
</template>

<script>
  import moment from 'moment'

  export default {
    name: 'userInfo',
    data () {
      return {
        userInfo: {},
        partyInfo: {},
        hasPermission: true,
      }
    },
    created () {
      this.userInfo = this.$localStorage.get('userInfo')
      this.partyInfo = this.$localStorage.get('partyInfo')
      this.partyPrem();
    },
    methods: {
      getTime (createTime) {
        if (!createTime) {
          return ''
        }
        return moment(createTime).format('YYYY年MM月DD日')
      },
      // 判断有无党员权限
      partyPrem () {
        let data = {
        }
        this.fetchService.partyPrem(data).then(res => {
          if (res.code !== 0) {
            this.hasPermission = false //非党员没有权限
          }
          this.hasPermission = true //党员有权限
        })
      },
      showTimePicker(rdsj) {
        this.$createDatePicker({
          min: new Date(1921, 7, 23),
          max: new Date(),
          value: new Date(rdsj),
          onSelect: (selectedTime, selectedText, formatedTime) => {
            console.log(this.userInfo)
            var date = moment(selectedTime).format("YYYY-MM-DD");
            var params = {
              userId: this.userInfo.id,
              rdsj: date,
              // rdsjFormat: date,
            }
            this.fetchService.user_rdsjUpdate(params).then((res) => {
              if (res.code !== 0) {
                return this.tools.tip(res.message);
              }
              this.tools.tip("修改成功");
              // this.newmemorabilias = Date.now();

              this.userInfomation()
            });
          },
        }).show();
      },
      // 用户信息
      userInfomation () {
        this.fetchService.currentNew().then(res => {
          this.$store.state.userInfo = res.data
          this.$localStorage.set('userInfo', res.data)
          this.loadPartyInfo(this.userInfo.id)
        })
      },
      // 党员信息
      loadPartyInfo (userId) {
        return this.fetchService.partyInfo(userId).then(res => {
          if (res.code !== 0) {
            this.$localStorage.set('partyInfo', {})
            return tools.tip(res.message)
          }
          this.$localStorage.set('partyInfo', res.data)
          setTimeout(() => {
            this.userInfo = this.$localStorage.get('userInfo')
            this.partyInfo = this.$localStorage.get('partyInfo')
          }, 10);
        })
      },
      getSex (sex) {
        console.log('------------sex=', sex)
        if (!sex) {
          return ''
        }
        if (sex === 'Male'|| sex === '男') {
          return '男'
        } else if(sex === 'Female' || sex === '女') {
          return '女'
        } else {
          return sex
        }
      },
      go (path) {
        this.$router.push({path: path})
      },
      getAge (birthday) {
        if (!birthday || birthday === '') {
          return ''
        }
        birthday = this.getBirthDayTime(birthday)
        let birthdays = new Date(birthday.replace(/-/g, '/'))
        let d = new Date()
        let age =
          d.getFullYear() -
          birthdays.getFullYear() -
          (d.getMonth() < birthdays.getMonth() ||
          (d.getMonth() === birthdays.getMonth() &&
            d.getDate() < birthdays.getDate())
            ? 1
            : 0)
        return age
      },
      getBirthDayTime (createTime) {
        return moment(createTime).format('YYYY-MM-DD HH:mm')
      }
    }
  }
</script>

<style scoped>

</style>
